package com.socialuni.sdk.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.socialuni.sdk.constant.NotifyType;
import com.socialuni.sdk.model.DO.comment.CommentDO;
import com.socialuni.sdk.model.DO.notify.NotifyDO;
import com.socialuni.sdk.model.DO.talk.TalkDO;
import com.socialuni.sdk.model.DO.talk.SocialTalkImgDO;
import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.repository.CommentRepository;
import com.socialuni.sdk.repository.TalkImgRepository;
import com.socialuni.sdk.repository.TalkRepository;
import com.socialuni.sdk.utils.CommentUtils;
import com.socialuni.sdk.utils.TalkUtils;
import com.socialuni.sdk.utils.UserUtils;
import com.socialuni.sdk.utils.common.JsonUtils;
import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author qinkaiyuan
 * @date 2019-09-26 16:00
 */
@Data@Component
public class UnreadNotifyVO {
    private static CommentRepository commentRepository;
    private static TalkImgRepository talkImgRepository;
    private static TalkRepository talkRepository;

    @Resource
    public void setTalkImgRepository(TalkImgRepository talkImgRepository) {
        UnreadNotifyVO.talkImgRepository = talkImgRepository;
    }

    @Resource
    public void setCommentRepository(CommentRepository commentRepository) {
        UnreadNotifyVO.commentRepository = commentRepository;
    }

    @Resource
    public void setTalkRepository(TalkRepository talkRepository) {
        UnreadNotifyVO.talkRepository = talkRepository;
    }

    private String talkId;
    private String talkUserId;
    private String avatar;
    private String nickname;
    private String content;
    private String replyContent;
    private String replyImg;
    private Date createTime;
    private Boolean vipFlag;
    private Boolean hasRead;

    public UnreadNotifyVO() {
    }

    public UnreadNotifyVO(UserDO user) {
        if (user != null) {
            this.nickname = user.getNickname();
            this.avatar = user.getAvatar();
            this.hasRead = false;
//            this.vipFlag = user.getVipFlag();
        }
    }

    public UnreadNotifyVO(NotifyDO notifyDO, UserDO user) {
        this(UserUtils.get(notifyDO.getUserId()));
        Integer commentId = notifyDO.getCommentId();

        CommentDO commentDO =  CommentUtils.get(commentId);
        this.content =  commentDO.getContent();
        this.createTime = commentDO.getCreateTime();

        TalkDO talk = TalkUtils.get(commentDO.getTalkId());

//        this.talkId = UnionIdDbUtil.createTalkUid(talk.getId(),  user);

        UserDO talkUser = UserUtils.get(talk.getUserId());

//        this.talkUserId = UnionIdDbUtil.createUserUid(talkUser.getId(),  user);
        this.hasRead = notifyDO.getHasRead();

        switch (notifyDO.getType()) {
            case NotifyType.talk_comment:
                List<SocialTalkImgDO> socialTalkImgDOS = talkImgRepository.findTop3ByTalkId(talk.getId());
//                List<TalkImgDO> talkImgDOS = talk.getImgs();
                if (socialTalkImgDOS.size() > 0) {
                    this.replyImg = socialTalkImgDOS.get(0).getSrc();
                } else {
                    this.replyContent = talk.getContent();
                }
                break;
            case NotifyType.comment_comment:
                CommentDO optionalCommentDO1 = CommentUtils.get(commentDO.getParentCommentId());
                this.replyContent = optionalCommentDO1.getContent();
                break;
            case NotifyType.reply_comment:
                CommentDO optionalCommentDO2 = CommentUtils.get(commentDO.getReplyCommentId());
                this.replyContent = optionalCommentDO2.getContent();
                break;
        }
    }

    public static List<UnreadNotifyVO> unreadNotifyDOToVOS(List<NotifyDO> notifyDOS,  UserDO user) {
        return notifyDOS.stream().map(item -> new UnreadNotifyVO(item,  user)).collect(Collectors.toList());
    }

    public TextMessage toMessage() {
        try {
            return new TextMessage(JsonUtils.objectMapper.writeValueAsString(this));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
